
#include "ioAT89S52.h"
#include "config.h"
#include "lcd_driver.h"
#include "util.h"
#include <math.h>

//内部驱动器S6B0108

//硬件配置
#define LCD_E P3_bit.P3_2
#define LCD_RW P3_bit.P3_3
#define LCD_DI P3_bit.P3_4
#define LCD_CSB P3_bit.P3_5
#define LCD_CSA P3_bit.P3_6
#define LCD_DATA P2

//#define LCD_RST_SUPPORT
//#define LCD_RST rstSig


void delay_while(uint8_t i)
{
    while (--i)
        ;
}

void lcd19264_read_busy()
{
    while (1)
    {
        LCD_E = 0;
        LCD_RW = 1;
        LCD_DI = 0;
        //      LCD_CSA = 0;
        //      LCD_CSB = 0;
        delay_while(3);
        LCD_E = 1;
        delay_while(3);
        LCD_DATA = 0xff;
        LCD_E = 0;
        delay_while(1);
        if (LCD_DATA & 0x80)
            break;
    }
}
void lcd19264_write_com(uint8_t com)
{
    lcd19264_read_busy();
    //  LCD_CSA = 0;//ѡ���ĸ�ģ��
    //  LCD_CSB = 0;
    LCD_E = 0;
    LCD_RW = 0;
    LCD_DI = 0;
    delay_while(1);
    LCD_E = 1;
    LCD_DATA = com;
    delay_while(1);
    LCD_E = 0;
    LCD_DI = 1;
    //  LCD_RW = 1;
}
void lcd19264_write_dat(uint8_t dat)
{
    lcd19264_read_busy();
    LCD_E = 0;
    LCD_RW = 0;
    LCD_DI = 1;
    //  LCD_CSA = 0;//ѡ���ĸ�ģ��
    //  LCD_CSB = 0;
    delay_while(1);
    LCD_E = 1;
    LCD_DATA = dat;
    delay_while(1);
    LCD_E = 0;
    LCD_DI = 0;
    //  LCD_RW = 1;
}
uint8_t lcd19264_read_dat()
{
    uint8_t value;
    lcd19264_read_busy();
    LCD_DATA = 0xff;
    LCD_E = 0;
    LCD_RW = 1;
    LCD_DI = 1;
    delay_while(3);
    LCD_E = 1;
    value = LCD_DATA; /////////////����ʱ����,������Ҫ��e����֮ǰ!!!
    delay_while(1);
    LCD_E = 0;
    //  value = LCD_DATA;
    delay_while(1);
    return value;
}
void lcd19264_select_block(uint8_t temp)
{
    //  temp &= 0x02;
    switch (temp)
    {
    case 0:
        LCD_CSA = 0;
        LCD_CSB = 0;
        break;
    case 1:
        LCD_CSA = 0;
        LCD_CSB = 1;
        break;
    case 2:
        LCD_CSA = 1;
        LCD_CSB = 0;
        break;
    default:
        break;
    }
}
void lcd19264_reset()
{
    #ifdef LCD_RST_SUPPORT
    LCD_RST = 0;
    delay_while(2);
    LCD_RST = 1;
    #endif
    DelayMS(1);
}

uint8_t g_targetLineNo = 0;
uint8_t g_targetX = 0;

void LCD19264SetLine(uint8_t lineNO)
{
  g_targetLineNo = lineNO;
  lcd19264_write_com(0xB8 | (0x7 & lineNO));
}

void LCD19264SetX(uint8_t x){
  g_targetX = x;
  uint8_t block = x / 64;
  lcd19264_select_block(block);
  //写起始X地址
  lcd19264_write_com(0x40 | (0x3f & (x % 64)));
  
  LCD19264SetLine(g_targetLineNo);
}

void LCD19264Write(uint8_t state)
{
    lcd19264_write_dat(state);
    g_targetX ++;
    if(g_targetX == 64){
      LCD19264SetX(g_targetX);
    }
    else if(g_targetX == 128) {
      LCD19264SetX(g_targetX);
    }
    if( g_targetX == 192){
      g_targetX = 0;            
      if( g_targetLineNo + 1 >= 8 ){
        g_targetLineNo = 0;
      }
      else {
        g_targetLineNo++;
      }
      LCD19264SetX(g_targetX);
    }
}


void LCD19264DisplayOnOff(bool status) {
  if(status) {
    lcd19264_write_com(0x3f);
  }
  else {
    lcd19264_write_com(0x3E);
  }
}

void lcd19264_init()
{
    uint8_t i;
    lcd19264_reset();
    lcd19264_clear_screen();
    for (i = 0; i < 3; i++)
    {
        //��ʼ����ʱ����������������ú�,����ֻҪ�˹��趨ҳ��ַ����,�к�y��ַ������
        lcd19264_select_block(i);
        LCD19264DisplayOnOff(true); //开启显示
        lcd19264_write_com(0xc0); //设置起始行地址
        lcd19264_write_com(0xb8); //初始化X=0
        lcd19264_write_com(0x40); //初始化Line=0
    }
}

void lcd19264_clear_screen()
{
    for (uint8 blockIndex = 0; blockIndex < 3; blockIndex++)
    {
        lcd19264_select_block(blockIndex);
        for (uint8 pageIndex = 0; pageIndex < Y_MAX / 8; pageIndex++)
        {
            lcd19264_write_com(0xb8 + pageIndex);
            for (uint8 i = 0; i < BLOCK_WITH; i++)
            {
                lcd19264_write_dat(0);
            }
        }
    }
}

/*
//x0,y0��ʾ����,state=1 ����.state=0 Ϩ��
void lcd19264_draw_point(uint8_t x0, uint8_t y0, uint8_t state)
{
    uint8_t temp, result, temp_add;
    //  x0 -= 1;    y0 -= 1;
    if (x0 >= 128)
    { //������
        x0 -= 128;
        lcd19264_select_block(2);
    }
    else if (x0 >= 64)
    { //������
        x0 -= 64;
        lcd19264_select_block(1);
    }
    else
    { //������
        //  x0 -= 1;
        lcd19264_select_block(0);
    }

    if (y0 >= 56)
    { //�����һҳ
        y0 -= 56;
        temp_add = 0xbf;
    }
    else if (y0 >= 48)
    { //�ڵ�7ҳ
        y0 -= 48;
        temp_add = 0xbe;
    }
    else if (y0 >= 40)
    { //��6ҳ
        y0 -= 40;
        temp_add = 0xbd;
    }
    else if (y0 >= 32)
    { //��5ҳ
        y0 -= 32;
        temp_add = 0xbc;
    }
    else if (y0 >= 24)
    { //��4ҳ
        y0 -= 24;
        temp_add = 0xbb;
    }
    else if (y0 >= 16)
    { //��3ҳ
        y0 -= 16;
        temp_add = 0xba;
    }
    else if (y0 >= 8)
    { //��2ҳ
        y0 -= 8;
        temp_add = 0xb9;
    }
    else
    { //��1ҳ
        temp_add = 0xb8;
    }
    //  lcd19264_write_com(0x3f);//����ʾ
    lcd19264_write_com(0xc0);      //�趨��ʼ��
    lcd19264_write_com(temp_add);  //����y��ַ////ҳ��ַ
    lcd19264_write_com(0x40 + x0); //����x��ַ////y��ַ
                                   //  lcd19264_write_com(0xc0);//�趨��ʼ��//////////////////////////////
    lcd19264_read_dat();           //��֪Ϊ�δ˴������һ�ն�
    temp = lcd19264_read_dat();    //��������ԭ�ȵ�����
    lcd19264_write_com(0xc0);      //�趨��ʼ��
    lcd19264_write_com(temp_add);  //��ַ��λ
    lcd19264_write_com(0x40 + x0); //��ַ��λ

    if (state)
        result = (temp | (1 << y0));
    else
        result = (temp & (~(1 << y0)));
    lcd19264_write_dat(result);
}
void lcd19264_draw_line(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint8_t state)
{
    int dx, dy;            //ע��˴�Ӧ�ö����int��!!!dx��dy���ܴ���128
    int d_flag;            //�����м����
    int dx_x2, dy_x2;      //����dx,dy��������,Ϊ�˼ӿ������ٶ�
    char dx_flag, dy_flag; //����仯����־λ

    dx = x1 - x0;
    dy = y1 - y0;

    if (dx > 0)
        dx_flag = 1;
    else
        dx_flag = -1;

    if (dy > 0)
        dy_flag = 1;
    else
        dy_flag = -1;

    if (x0 == x1)
    { //���Ǵ�ֱ��
        while (y0 != y1)
        {
            lcd19264_draw_point(x0, y0, state);
            y0 += dy_flag;
        }
        lcd19264_draw_point(x0, y0, state);
    }
    else if (y0 == y1)
    { //����ˮƽ��
        while (x0 != x1)
        {
            lcd19264_draw_point(x0, y0, state);
            x0 += dx_flag;
        }
        lcd19264_draw_point(x0, y0, state);
    }
    else
    { //˵����б��
        dx = dx * dx_flag;
        dy = dy * dy_flag; //ȡ����ֵ
        dx_x2 = dx * 2;
        dy_x2 = dy * 2; //������������,�ӿ������ٶ�
        if (dx >= dy)
        { //��x��Ϊ��׼
            d_flag = dy_x2 - dx;
            while (x0 != x1)
            {
                lcd19264_draw_point(x0, y0, state);
                x0 = x0 + dx_flag;
                if (d_flag < 0)
                {
                    d_flag = d_flag + dy_x2; //������һ������ֵ
                }
                else
                {
                    d_flag = d_flag + (dy_x2 - dx_x2);
                    y0 += dy_flag;
                }
            }
            lcd19264_draw_point(x0, y0, state); //�����һ��
        }
        else
        { //��y��Ϊ��׼
            d_flag = dx_x2 - dy;
            while (y0 != y1)
            {
                lcd19264_draw_point(x0, y0, state);
                y0 += dy_flag;
                if (d_flag < 0)
                {
                    d_flag += dx_x2;
                }
                else
                {
                    d_flag += dx_x2 - dy_x2;
                    x0 += dx_flag;
                }
            }
            lcd19264_draw_point(x0, y0, state);
        }
    }
}
*/



